/**
 * 
 */
package dao;

import java.util.List;

import model.User;

import org.hibernate.Session;
import org.hibernate.Transaction;

import br.com.caelum.vraptor.ioc.Component;

/**
 * @author jlucasps
 *
 */
@Component
public class UserDAO {

	Session session;

	/**
	 * Constructor.
	 * @param session
	 */
	public UserDAO(Session session) {
		this.session = session;
	}

	public void delete(User user) {
		Transaction tx = this.session.beginTransaction();
		this.session.delete(user);
		tx.commit();
	}

	public void save(User user){
		Transaction tx = this.session.beginTransaction();
		this.session.save(user);
		tx.commit();
	}
	
	public User load(Long id){
		Transaction tx = this.session.beginTransaction();
		User user = (User) this.session.load(User.class, id);
		tx.commit();
		return user;
	}

	public void update(User user){
		Transaction tx = this.session.beginTransaction();
		this.session.update(user);
		tx.commit();
	}

	@SuppressWarnings("unchecked")
	public List<User> list() {
		return this.session.createCriteria(User.class).list();
	}
}
